<template>
  <!--编辑商品管理-->
  <div class="container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>添加商品</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="back">返回</el-button>
      </div>
      <el-form ref="addForm" v-model="editGoodForm" label-width="120px">
        <el-form-item label="品牌名称">
          <el-input v-model="editGoodForm.name" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="商品图片">
          <el-upload
            action="/oss/img"
            :file-list="fileList"
            list-type="picture-card"
            name="object"
            :on-success="uploadSuccess"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="this.dialogImg" alt="">
          </el-dialog>
        </el-form-item>
        <el-form-item label="品牌类型">
          <el-radio-group v-model="editGoodForm.type">
            <el-radio :label="1">国产</el-radio>
            <el-radio :label="0">进口</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">更新</el-button>
          <el-button @click="back">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { editBranList, queryBranList } from '@/api/goodBrand'

export default {
  name: 'editGoodBand',
  data() {
    return {
      editGoodForm: {},
      //上传图片数据
      fileList: [],
      dialogVisible: false,
      dialogImg: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList(){
      //  获取当前商品品牌ID数据
      const  id=this.$route.query.id
      queryBranList(id).then(res=>{
        this.editGoodForm=res.data
        this.fileList.push({url:res.data.image})
      })
    },
    back() {
      this.$store.dispatch('tagsView/delVisitedView', this.$route)
      this.$router.push('/goods/goodbrand')
    },
    uploadSuccess(file, fileList) {
      this.fileList.push({ url: fileList.response.data })
    },
    //移除上传图片数据
    handleRemove(file, fileList) {
      this.fileList=fileList
    },
    handlePictureCardPreview(file) {
      this.dialogVisible = true
      this.dialogImg = file.url
    },

    onSubmit(){
      editBranList({
        id:this.$route.query.id,
        name:this.editGoodForm.name,
        type:this.editGoodForm.type,
        image:this.fileList[0].url
      }).then(res=>{
        this.$message.success('更新成功')
        this.back()
      })
    }
  }
}
</script>

<style scoped>

</style>
